<?php


namespace app\admin\controller;
use think\facade\Request;
use app\common\model\Config as C;
use app\common\model\ConfigGroup as CG;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;

class Config extends Base
{

     public function index(){
        $pid = Request::param('pid'); //类别pid
        $group_id = Request::param('group_id');
        //dump($group_id);die;
        $where = Request::param('where', []);
        $where = array_filter($where);
        //dump($where);die;
        $keyword = Request::param('keyword');
        $prefix = config('database.prefix');
        $list = Db::name('config')
            ->where('group_id',$group_id)
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->whereLike('config_title', "%$keyword%");
                }
            })
            //->whereIn('group_id',$group_id)
            ->order('config_sort asc')
            ->paginate(10, false, ['query' => request()->param()]);
        //dump($list);die;
        $page = $list->render();
        $view = [
            'keyword' => $keyword,
            'page' => $page,
            'list' => $list,
            'pid' => $pid,
            'group_id'=>$group_id,
        ];
        View::assign($view);
        return View::fetch();
     }

     public function add(){
         $group_one = Db::name('config_group')->find(['group_id'=>Request::param('group_id')]);
         if(empty($group_one)){
             $this->error('该配置组id不存在');
         }
         if(Request::isPost()){
             $data = Request::post();
             $data['add_time'] = time();
             $data['add_user_id'] = Session::get('admin.admin_id');
             if (C::create($data) !==false) {
                 $this->success('添加配置参数名称:' . $data['config_title'].'添加成功', 'index?group_id='.Request::param('group_id'));
             }else{
                 $this->error('添加失败','index?group_id='.Request::param('group_id'));
             }
         }else{
             $view = [
                 'group_id' => Request::param('group_id'),
             ];
         }
         View::assign($view);
         return View::fetch();
     }

     public function edit(){
         $config_one = Db::name('config')->find(['config_id'=>Request::param('config_id')]);
         if (empty($config_one)) {
             $this->error('配置参数名称id不存在');
         }
         if(Request::isPost()){
             $data = Request::post();
             $data['add_time'] = time();
             $data['add_user_id'] = Session::get('admin.admin_id');
             $where['config_id'] = Request::param('config_id');
             if (C::update($data,$where) !==false) {
                 $this->success('编辑配置参数名称:' . $data['config_title'].'编辑成功', 'index?group_id='.Request::param('group_id'));
             }else{
                 $this->error('修改失败', 'index?group_id='.Request::param('group_id'));
             }
         }else{
             $view = [
                 'group_id' => $config_one['group_id'],
                 'config_one'=>$config_one,
             ];
         }
         View::assign($view);
         return View::fetch();
     }

     public function del(){
        $config_id = Request::param('config_id');
        if (Db::name('config')->delete($config_id)){
            return success('删除配置参数成功！','/config/index?group_id='.Request::param('group_id'));
        }else{
            return error('删除配置参数失败！');
        }
     }

    public function pildel(){
        $id = Request::param('id');
        $group_id = Db::name('config')->where('config_id',$id)->field('group_id')->find();
        //dump($group_id);die;
        $ids = explode(',',$id);
        foreach ($ids as $k => $v){
            $res=Db::name('config')->delete($v);
        }
        if ($res){
            return success('批量删除配置参数成功！','/config/index?group_id=$group_id');
        }else{
            return error('批量删除配置参数失败！');
        }
    }


    public function sort()
    {
        if (Request::isPost()) {
            $config_id = Request::param('config_id');
            $config_sort = Request::param('config_sort');
            //dump($config_sort);die;
            if(Db::name('config')->where(array('config_id'=>$config_id))->update(['config_sort' =>$config_sort])){
                $this->success('排序成功');
            }else{
                $this->error('没有改变参数');
            }
        }
    }



    public function  webConfig(){
        $cfmodel = new \app\common\model\Config();

            if(!$page_data = cache('config_page_data')){
                $where = array();
                $where[] = array('group_status','=',1);
                $page_data = $cfmodel->webConfig($where);
                cache('config_page_data,',$page_data,86400);
            }
            $view= [
                'page_data' => $page_data,
                'info'=>'null'
            ];


        View::assign($view);
        return View::fetch();
    }

    public function webConfigdata(){
        $cfmodel = new \app\common\model\Config();
        if(Request::isPost()){
            $data = $_POST['config'];
            if(!is_array($data)){
                $this->error('数据不能为空');
            }

            foreach ($data as $k=>$v){
                $cfmodel->saveData(array('config_id'=>$k),$v);
            }
            $this->success('修改成功');
        }
    }


    public function ajaxConfigName ()
    {
        $key = input('name');
        $value = input('param');
        $config_id = input('config_id', 0, 'intval');
        $config_info = new \app\common\model\Config();
        $res = $config_info->ajaxConfigName($value, $config_id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '配置参数英文名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }
    /**
     * ajax验证组英文名称
     */
    public function ajaxConfigTitle ()
    {
        $key = input('name');
        $value = input('param');
        $config_id = input('config_id', 0, 'intval');
        $config_info = new \app\common\model\Config();
        $res = $config_info->ajaxConfigTitle($value, $config_id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '配置参数中文名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }

}